﻿@using Orchard.Core.Shapes.Localization
@using System.Globalization

@{
    
    // prevent the shape from being rendered twice in a page
    if (WorkContext.GetState<object>("DatePickerLocalization") != null) {
        return;
    }

    WorkContext.SetState("DatePickerLocalization", new object());
    
    Style.Require("jQueryUI_DatePicker");
    Script.Require("jQueryUI_DatePicker");

    var cultureInfo = CultureInfo.GetCultureInfo(WorkContext.CurrentCulture);
    
    var dateTimeLocalization = WorkContext.Resolve<IDateTimeLocalization>();
    var monthNames = FormatJsList(dateTimeLocalization.MonthNames.Text);
    var monthNamesShort = FormatJsList(dateTimeLocalization.MonthNamesShort.Text);
    var dayNames = FormatJsList(dateTimeLocalization.DayNames.Text);
    var dayNamesShort = FormatJsList(dateTimeLocalization.DayNamesShort.Text);
    var dayNamesMin = FormatJsList(dateTimeLocalization.DayNamesMin.Text);

    // convert .NET format into jQuery format
    // http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx
    // http://docs.jquery.com/UI/Datepicker/formatDate

    var dateFormat = cultureInfo.DateTimeFormat.ShortDatePattern
        .Replace("yyyy", "YY")
        .Replace("yy", "Y")
        .Replace("Y", "y")
        .Replace("M", "m")
        ;
}

@functions {

    private string FormatJsList(string csv) {
        return "'" + String.Join("', '", csv.Split(',').Select(x => x.Trim())) + "'";
    }

}
<script type="text/javascript">
//<![CDATA[
    $(function() {
        $.datepicker.regional[''] = {
            closeText: '@T("Done")', // Display text for close link
            prevText: '@T("Prev")', // Display text for previous month link
            nextText: '@T("Next")', // Display text for next month link
            currentText: '@T("Today")', // Display text for current month link
            monthNames: [@Html.Raw(monthNames)], // Names of months for drop-down and formatting
            monthNamesShort: [@Html.Raw(monthNamesShort)], // For formatting
            dayNames: [@Html.Raw(dayNames)], // For formatting
            dayNamesShort: [@Html.Raw(dayNamesShort)], // For formatting
            dayNamesMin: [@Html.Raw(dayNamesMin)], // Column headings for days starting at Sunday
            weekHeader: '@T("Wk")', // Column header for week of the year
            dateFormat: '@dateFormat', // See format options on parseDate
            firstDay: @dateTimeLocalization.FirstDay, // The first day of the week, Sun = 0, Mon = 1, ...
            isRTL: @(cultureInfo.TextInfo.IsRightToLeft ? "true" : "false"), // True if right-to-left language, false if left-to-right
            showMonthAfterYear: @(dateTimeLocalization.ShowMonthAfterYear ? "true" : "false"), // True if the year select precedes month, false for month then year
            yearSuffix: '@dateTimeLocalization.YearSuffix' // Additional text to append to the year in the month headers
        };

        $.datepicker.setDefaults($.datepicker.regional['']);
    })
//]]>
</script>